/**
 * Created by yuris on 16.12.14.
 */
package ShopObjects {
import ShopObjects.AbstractGameObjectView;

import dragonBones.Armature;
import dragonBones.animation.Animation;
import dragonBones.animation.WorldClock;
import dragonBones.core.DragonBones;


import starling.display.Sprite;
import starling.events.Event;

public class CashDesk1View extends AbstractGameObjectView {
    private  var _armature:Armature;
    protected var _currentAnimation:String = "still";
    public function CashDesk1View() {
        super();
        this._armature = GraphicFactory.createCashDeskArmature(1);
        this.addChild(this._armature.display as Sprite);
        this._changeAnimation("still");

        this.addEventListener(Event.ADDED_TO_STAGE, this.HANDLER_addedToStage);
        this.addEventListener(Event.REMOVED_FROM_STAGE, this.HANDLER_removedToStage);
    }

    override public function set angle(angle:int):void {
        super.angle = angle;
        this._armature.advanceTime(-1);
    }

    override protected function _onRotate45():void{
        this._armature.display.scaleX = 1;
        this._changeAnimation("still");
    }
    override protected function _onRotate135():void{
        this._armature.display.scaleX = -1;
        this._changeAnimation("still");
    }
    override protected function _onRotate225():void{
        this._armature.display.scaleX = 1;
        this._changeAnimation("still");
    }
    override protected function _onRotate315():void{
        this._armature.display.scaleX = -1;
        this._changeAnimation("still");
    }

    protected function _changeAnimation(label:String):void {
        //this.unflatten();

        this._currentAnimation = label;
        this._armature.animation.gotoAndPlay(label);
        this._armature.advanceTime(-1);

        //this.flatten();
    }

    private var _currentFaceAnimation:String = "no_emoj";
    private const _faceAnimations:Array =    [
                                                "no_emoj",
                                                "smile_lite",
                                                //"o_O1",
                                                "smile_norm",
                                                "dream1",
                                                "smile_good"
                                            ];
    public function changeCashManFaceAnimation():void {
        var headAnimation:Animation = this._armature.getBone("CashMan").childArmature.getBone("Head").childArmature.animation;
        this._currentFaceAnimation = this._faceAnimations[int(Math.random()*_faceAnimations.length)];
        headAnimation.gotoAndPlay(this._currentFaceAnimation);
    }

    public function updateFaceAnimation():void {
        var headAnimation:Animation = this._armature.getBone("CashMan").childArmature.getBone("Head").childArmature.animation;
        headAnimation.gotoAndPlay(this._currentFaceAnimation);
    }

    override public function get typemodel():String{
        return "d"+"1";
    }


    private function HANDLER_addedToStage(event:Event):void {
        WorldClock.clock.add(this._armature);
    }
    private function HANDLER_removedToStage(event:Event):void {
        WorldClock.clock.remove(this._armature);
    }
}
}
